/**
 *
 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 * 
 **/
package lucee.runtime.db.driver.state;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import lucee.runtime.PageContext;
import lucee.runtime.PageContextImpl;
import lucee.runtime.debug.ActiveQuery;

public final class StateUtil {

	public static boolean execute(PageContext pc, Statement stat, String sql) throws SQLException {
		if (pc == null) return stat.execute(sql);
		PageContextImpl pci = (PageContextImpl) pc;
		try {
			setActiveStatement(pci, stat, sql);
			return stat.execute(sql);
		}
		finally {
			pci.releaseActiveQuery();
		}
	}

	public static boolean execute(PageContext pc, Statement stat, String sql, int autoGeneratedKeys) throws SQLException {
		if (pc == null) return stat.execute(sql, autoGeneratedKeys);
		PageContextImpl pci = (PageContextImpl) pc;
		try {
			setActiveStatement(pci, stat, sql);
			return stat.execute(sql, autoGeneratedKeys);
		}
		finally {
			pci.releaseActiveQuery();
		}
	}

	public static boolean execute(PageContext pc, Statement stat, String sql, int[] columnIndexes) throws SQLException {
		if (pc == null) return stat.execute(sql, columnIndexes);
		PageContextImpl pci = (PageContextImpl) pc;
		try {
			setActiveStatement(pci, stat, sql);
			return stat.execute(sql, columnIndexes);
		}
		finally {
			pci.releaseActiveQuery();
		}
	}

	public static boolean execute(PageContext pc, Statement stat, String sql, String[] columnNames) throws SQLException {
		if (pc == null) return stat.execute(sql, columnNames);
		PageContextImpl pci = (PageContextImpl) pc;
		try {
			setActiveStatement(pci, stat, sql);
			return stat.execute(sql, columnNames);
		}
		finally {
			pci.releaseActiveQuery();
		}
	}

	public static ResultSet executeQuery(PageContext pc, Statement stat, String sql) throws SQLException {
		if (pc == null) return stat.executeQuery(sql);
		PageContextImpl pci = (PageContextImpl) pc;
		try {
			setActiveStatement(pci, stat, sql);
			return stat.executeQuery(sql);
		}
		finally {
			pci.releaseActiveQuery();
		}
	}

	public static int executeUpdate(PageContext pc, Statement stat, String sql) throws SQLException {
		if (pc == null) return stat.executeUpdate(sql);
		PageContextImpl pci = (PageContextImpl) pc;
		try {
			setActiveStatement(pci, stat, sql);
			return stat.executeUpdate(sql);
		}
		finally {
			pci.releaseActiveQuery();
		}
	}

	public static int executeUpdate(PageContext pc, Statement stat, String sql, int autoGeneratedKeys) throws SQLException {
		if (pc == null) return stat.executeUpdate(sql, autoGeneratedKeys);
		PageContextImpl pci = (PageContextImpl) pc;
		try {
			setActiveStatement(pci, stat, sql);
			return stat.executeUpdate(sql, autoGeneratedKeys);
		}
		finally {
			pci.releaseActiveQuery();
		}
	}

	public static int executeUpdate(PageContext pc, Statement stat, String sql, int[] columnIndexes) throws SQLException {
		if (pc == null) return stat.executeUpdate(sql, columnIndexes);
		PageContextImpl pci = (PageContextImpl) pc;
		try {
			setActiveStatement(pci, stat, sql);
			return stat.executeUpdate(sql, columnIndexes);
		}
		finally {
			pci.releaseActiveQuery();
		}
	}

	public static int executeUpdate(PageContext pc, Statement stat, String sql, String[] columnNames) throws SQLException {
		if (pc == null) return stat.executeUpdate(sql, columnNames);
		PageContextImpl pci = (PageContextImpl) pc;
		try {
			setActiveStatement(pci, stat, sql);
			return stat.executeUpdate(sql, columnNames);
		}
		finally {
			pci.releaseActiveQuery();
		}
	}

	public static boolean execute(PageContext pc, PreparedStatement stat, String sql) throws SQLException {
		if (pc == null) return stat.execute();
		PageContextImpl pci = (PageContextImpl) pc;
		try {
			setActiveStatement(pci, stat, sql);
			return stat.execute();
		}
		finally {
			pci.releaseActiveQuery();
		}
	}

	public static ResultSet executeQuery(PageContext pc, PreparedStatement stat, String sql) throws SQLException {
		if (pc == null) return stat.executeQuery();
		PageContextImpl pci = (PageContextImpl) pc;
		try {
			setActiveStatement(pci, stat, sql);
			return stat.executeQuery();
		}
		finally {
			pci.releaseActiveQuery();
		}
	}

	public static int executeUpdate(PageContext pc, PreparedStatement stat, String sql) throws SQLException {
		if (pc == null) return stat.executeUpdate();
		PageContextImpl pci = (PageContextImpl) pc;
		try {
			setActiveStatement(pci, stat, sql);
			return stat.executeUpdate();
		}
		finally {
			pci.releaseActiveQuery();
		}
	}

	private static void setActiveStatement(PageContextImpl pc, Statement stat, String sql) {
		pc.setActiveQuery(new ActiveQuery(sql, System.currentTimeMillis()));
	}
}